/*
		[Leo.C, Studio] (C)2004 - 2008
		
   		$Hanization: LeoChung $
   		$E-Mail: who@imll.net $
   		$HomePage: http://imll.net $
   		$Date: 2008/11/8 18:02 $
*/
/* Demo Note:  This demo uses a FileProgress class that handles the UI for displaying the file name and percent complete.
这演示使用FileProgress的类来处理文件的名称和完成百分比显示的UI。
The FileProgress class is not part of SWFUpload.
FileProgress类是不是的SWFUpload部分。
*/


/* **********************
   Event Handlers
   These are my custom event handlers to make my
   web application behave the way I went when SWFUpload
   completes different tasks.  These aren't part of the SWFUpload
   package.  They are part of my application.  Without these none
   of the actions SWFUpload makes will show up in my application.
   事件处理程序
   这些都是我的自定义事件处理程序，使我
   Web应用程序的行为方式，我去的时候SWFUpload
   完成不同的任务。这些都不是SWFUpload
   包。他们是我的应用程序的一部分。如果没有这些都不
   行动SWFUpload将显示在我的应用程序。
   ********************** */
//创建上传的div
function createUploadDiv(file,self){
	var progressTarget=self.customSettings.progressTarget;
	var width=self.customSettings.width;
	var height=self.customSettings.height;
	var div='<div class="'+self.customSettings.divClass+'" id="'+file.id+'">';
		div+='<a href="javascript:void(0);" onclick="cancelUpload(\''+file.id+'\')" class="close"><span>X</span></a>';
		div+='</div>';
	$('#'+progressTarget).append(div);
}
//中断上传
function cancelUpload(fileid,self){
	//self.cancelUpload(fileid);
	if(self){
		self.stopUpload();
	}
	$('#'+fileid).remove();
}
//删除已经上传的文件
function deleteFile(fileid,deleteFilePath,aid,afterDeleteFunction){
	$.getJSON(deleteFilePath,{aid:aid},function(data){
		if(data.err==1){
			uploadAlert(data.msg);
		}else{
			if(afterDeleteFunction){
				eval(afterDeleteFunction);
			}
			$('#'+fileid).remove();
		}
	})
}
function uploadAlert(content){
	art.dialog({
	    title: '上传提示',
	    content: content,
	    resize: false,
	    fixed: true,
	    lock: true
	});
}
//选择完成时调用
function fileQueued(file) {
	try {
		var progressTarget=this.customSettings.progressTarget;
		if($('#'+progressTarget).length!=1){
				uploadAlert('对不起！没有找到你要显示上传的位置，请检查配置。');
				return false;
			}
		createUploadDiv(file,this);
	} catch (ex) {
		this.debug(ex);
	}
}
function fileQueueError(file, errorCode, message) {
	try {
		if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
			uploadAlert("您正在上传的文件队列过多.\n" + (message === 0 ? "您已达到上传限制" : "您最多能选择 " + (message > 1 ? "上传 " + message + " 文件." : this.settings.file_upload_limit+"个文件.")));
			return;
		}

		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			uploadAlert(file.name+' 的文件大小超过'+this.settings.file_size_limit+',此文件上传已中断');
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			uploadAlert('文件'+file.name+' 大小为0字节，不允许上传');
			break;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			uploadAlert('对不起！你上传的文件（' + file.name +'）类型不在被允许的范围内');
			break;
		default:
			uploadAlert('对不起！你上传的文件（' + file.name +'）发生的未知错误');
			break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		this.startUpload();
	} catch (ex)  {
        this.debug(ex);
	}
}
//开始上传 上传的这个文件信息
function uploadStart(file) {
	try {
		
	}
	catch (ex) {}
	
	return true;
}

function uploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		/*if(this.customSettings.progressTarget)
		{//如果指定了需要显示的id 逐个显示上传的文件
			var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
			$('#'+file.id+' .loadingpercent').html(percent+'%');
		}*/
	} catch (ex) {
		this.debug(ex);
	}
}
//fileObj是文件对象，server_data是服务器返回的值 单个文件上传成功的时候调用
function uploadSuccess(file, serverData) {//alert(serverData);//uploadAlert(jsdebug(serverData))
	try {
		serverData=eval('(' + serverData + ')');
		if(parseInt(serverData.err)==1){
			//下面的代码处理页面的相关显示
			$('#'+file.id).html('').css('background','none');
			var html='<a href="javascript:void(0);" onclick="deleteFile(\''+file.id+'\',\''+this.customSettings.deleteFilePath+'\',\''+serverData.aid+'\',\''+this.customSettings.afterDeleteFunction+'\')" class="close"><span>X</span></a>';
			//设置显示的内容
			if(this.customSettings.showOption.filepath)
			html+='<div class="filepath"><img src="'+serverData.filepath+'"></div>';
			if(this.customSettings.showOption.filename)
			html+='<div class="filename">'+serverData.filename+'</div>';
			
			html+='<input type="hidden" value="'+serverData.aid+'" name="SWF_Aids[]">';
			//设置要提交的内容
			if(this.customSettings.hiddenOption.filepath)
			html+='<input type="hidden" value="'+serverData.filepath+'" name="'+this.customSettings.hiddenOption.filepath+'">';
			$('#'+file.id).append(html);
			if(this.customSettings.afterUploadFunction){
				eval(this.customSettings.afterUploadFunction);
			}
		}else{
			cancelUpload(file.id,this);
			uploadAlert(serverData.msg);
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadError(file, errorCode, message) {
	try {
		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
			this.debug("错误代码: HTTP错误, 文件名: " + file.name + ", 信息: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
			this.debug("错误代码: 上传失败, 文件名: " + file.name + ", 文件尺寸: " + file.size + ", 信息: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.IO_ERROR:
			this.debug("错误代码: IO 错误, 文件名: " + file.name + ", 信息: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
			this.debug("错误代码: 安全错误, 文件名: " + file.name + ", 信息: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			this.debug("错误代码: 超出上传限制, 文件名: " + file.name + ", 文件尺寸: " + file.size + ", 信息: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
			this.debug("错误代码: 文件验证失败, 文件名: " + file.name + ", 文件尺寸: " + file.size + ", 信息: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			// If there aren't any files left (they were all cancelled) disable the cancel button
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			break;
		default:
			this.debug("错误代码: " + errorCode + ", 文件名: " + file.name + ", 文件尺寸: " + file.size + ", 信息: " + message);
			break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}

function uploadComplete(file) {
	if (this.getStats().files_queued === 0) {
		
	}
	this.startUpload();
}
